// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include "MediaManager.h"

using namespace std;

MediaManager* MediaManager::_pInstance = 0;

MediaManager::MediaManager(void)
{
}

MediaManager::~MediaManager(void)
{
	for (map<string, BMAP*>::iterator i = _imageMap.begin();
		i != _imageMap.end(); ++i) {
			bmap_purge(i->second);
	}
	_imageMap.clear();

	for (map<string, SOUND*>::iterator i = _soundMap.begin();
		i != _soundMap.end(); ++i) {
			snd_remove(i->second);
	}
	_soundMap.clear();

	if (_pInstance != 0) delete _pInstance;
}

//singleton method
MediaManager* MediaManager::getInstance(void)
{
	if(_pInstance==0) {
		_pInstance = new MediaManager;
	}

	return _pInstance;
}

// returns an BMAP* corresponding to the given file name
// loads the image if necessary
BMAP* MediaManager::getImage(const string& imageName)
{
	map<string, BMAP*>::iterator i = _imageMap.find(imageName);
	if (i != _imageMap.end()) {
		return i->second;
	} else {
		BMAP* newImage = bmap_create(const_cast<char*>(imageName.c_str()));
		_imageMap.insert(make_pair(imageName, newImage));

		return newImage;
	}
}

// returns an SOUND* corresponding to the given file name
// loads the soundfile if necessary
SOUND* MediaManager::getSound(const string& soundName)
{
	map<string, SOUND*>::iterator i = _soundMap.find(soundName);
	if (i != _soundMap.end()) {
		return i->second;
	} else {
		SOUND* newSound = snd_create(const_cast<char*>(soundName.c_str()));
		_soundMap.insert(make_pair(soundName, newSound));

		return newSound;
	}
}
